package com.zzq.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.zzq.annotation.EnableMyPage;
import com.zzq.domain.search.StaticResourcesSearchParamet;
import com.zzq.result.SmileResult;
import com.zzq.domain.entity.SmileStaticResources;
import com.zzq.service.SmileStaticResourcesService;
import com.zzq.service.StaticeResourcesService;
import com.zzq.util.OssFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class  StaticeResourcesServiceImpl implements StaticeResourcesService {
    @Autowired
    SmileStaticResourcesService staticResourcesService;

    @Autowired
    OssFileUtil ossFileUtil;
    @Override
    public SmileResult addResources(MultipartFile file, String name, String dir) throws IOException, ClientException {
        String s = ossFileUtil.uploadStaticFile(file, dir,name);
        String staticFileUrl = ossFileUtil.getStaticFileUrl(s);
        SmileStaticResources smlieStaticResources = new SmileStaticResources();
        smlieStaticResources.setName(name);
        smlieStaticResources.setType("img");
        smlieStaticResources.setDir(dir);
        smlieStaticResources.setUrl(staticFileUrl);
        staticResourcesService.save(smlieStaticResources);
        return SmileResult.sucess("上传成功",staticFileUrl);
    }
    @Override
    @EnableMyPage
    public SmileResult getALlResources(StaticResourcesSearchParamet searchParamet)  {

        List<SmileStaticResources> list = staticResourcesService.list();
        return SmileResult.sucess("返回成功",list);
    }

    @Override
    public SmileResult delResources(Long staticResourcesId) {
        staticResourcesService.removeById(staticResourcesId);
        return SmileResult.sucess("删除成功");
    }


}
